import java.util.ArrayList;
import java.util.List;

public class StringExtractor {

    public static void main(String[] args) {
        // 示例测试字符串，包含双引号和转义字符
        String sourceCode = "\"我是薛威\\\"我的软件工程老师是杨青！\\\"\"";
        List<String> extractedStrings = extractStrings(sourceCode);

        // 检查提取到的字符串内容
        if (extractedStrings.isEmpty()) {
            System.out.println("未找到任何字符串");
        } else {
            System.out.println("提取到的字符串:");
            extractedStrings.forEach(System.out::println);
        }
    }

    public static List<String> extractStrings(String sourceCode) {
        List<String> result = new ArrayList<>();
        StringBuilder currentString = new StringBuilder();
        String state = "Start";

        for (int i = 0; i < sourceCode.length(); i++) {
            char c = sourceCode.charAt(i);
            System.out.println("当前状态: " + state + ", 当前字符: " + c);

            switch (state) {
                case "Start":
                    if (c == '"') {
                        state = "InString";
                        currentString.setLength(0);  // 开始新字符串
                    }
                    break;

                case "InString":
                    if (c == '\\') {
                        state = "Escape";  // 转义字符状态
                    } else if (c == '"') {
                        state = "EndString";  // 字符串结束
                    } else {
                        currentString.append(c);  // 添加字符
                    }
                    break;

                case "Escape":
                    currentString.append(c);  // 直接添加被转义字符
                    state = "InString";  // 返回字符串读取状态
                    break;

                case "EndString":
                    System.out.println("提取到字符串: " + currentString);
                    result.add(currentString.toString());
                    state = "Start";  // 准备下一个字符串
                    currentString.setLength(0);
                    break;
            }
        }

        return result;
    }
}
